package gameheart.client.logic;

import gameheart.client.sprite.ImageResposity;
import gameheart.client.sprite.LabelSprite;
import gameheart.client.sprite.ImageSprite;
import gameheart.transport.MyException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PlayCanvas extends State {

    public PlayState _state;
    public Point[] _deltaVector = new Point[4];
    public Point _centerPointOfPlayer[] = {new Point(360, 440), new Point(240, 370), new Point(350, 290), new Point(480, 360)};
    public Point _centerPoint = new Point(380, 370);
    public Point[] _beginLocationOfPlayers = {new Point(190, 555), new Point(85, 180), new Point(530, 175), new Point(633, 540)};
    private ImageSprite _bg;

    public Point getMousePoint() {
        Point positon = _mainGame.getMousePosition();
        if (positon != null) {
            positon.x = positon.x - _mainGame.getInsets().left;
            positon.y = positon.y - _mainGame.getInsets().top;
        }
        return positon;
    }
    public ImageResposity _imageResposity;
    private ArrayList<PlayState> _statePrototypes = null;
    //ArrayList<LabelSprite> _menus = new ArrayList<LabelSprite>();

    public PlayCanvas(MainGame mainGame, ClientAdapter adapter) {
        super(mainGame);
        _mainGame.setAdapter(adapter);
        _imageResposity = new ImageResposity();

        _deltaVector = new Point[4];
        _deltaVector[0] = new Point(28, 0);
        _deltaVector[1] = new Point(0, 25);
        _deltaVector[2] = new Point(-25, 0);
        _deltaVector[3] = new Point(0, -25);
    }

    public void changeState(PlayState.playStateType stateType) {
        for (PlayState state : _statePrototypes) {
            if (state.getStateType() == stateType) {
                _state = state.Clone();
                _state.Init();
                break;
            }
        }
        System.out.print(stateType.toString());
    }

    public void gameUpdate() {
        try {
            _mainGame.getAdapter().processMessage();
        } catch (MyException ex) {
            Logger.getLogger(PlayCanvas.class.getName()).log(Level.SEVERE, null, ex);
        }
//        if(_adapter.IsGameEnd())
//        {
//            _mainGame.ChangeGameState(GameStateIds.EndGameState);
//            
//        }
        _pointInHandSprite._text = "Point in hand : " + String.valueOf(_mainGame.getAdapter().getPointInHand());
        if (_mainGame.getAdapter().IsHavePlayRequest()) {
            _notifySprite._text = "let's play";
        } else {
            _notifySprite._text = "";
        }
        _state.update();
    }
    private SynMouseRect mouseRect = new SynMouseRect();

    public void gameRender(Graphics g) {
        _bg.draw(g);
//        for (int index = 0; index < _menus.size(); ++index) {
//            _menus.get(index).draw(g);
//        }
        for (int index = 0; index < 4; ++index) {
            _names[index].draw(g);
            _scores[index].draw(g);
        }
        _pointInHandSprite.draw(g);
        _notifySprite.draw(g);
        _state.render(g);
    }
    private LabelSprite _names[] = new LabelSprite[4];
    private LabelSprite _scores[] = new LabelSprite[4];
    private LabelSprite _pointInHandSprite;
    private LabelSprite _notifySprite;
    int[] _Xs = {900, 2, 385, 735};
    int[] _Ys = {150, 430, 103, 430};

    public void gameInit() {
        _statePrototypes = new ArrayList<PlayState>();
        _statePrototypes.add(new DealingState(this));
        _statePrototypes.add(new PassingCardState(this));
        _statePrototypes.add(new HandState(this));
//        BufferedImage menuBackGround = _imageResposity.getImage("menu.jpg");
//        _menus.add(new LabelSprite(800, 425, 300, 30, "Create game", menuBackGround));
//        _menus.add(new LabelSprite(800, 465, 300, 30, "Join game", menuBackGround));
//        _menus.add(new LabelSprite(800, 505, 300, 30, "Help", menuBackGround));
//        _menus.add(new LabelSprite(800, 545, 300, 30, "Exit", menuBackGround));

        // BufferedImage bg = _imageResposity.getImage("bg_main.jpg");
        _bg = new ImageSprite(0, 0, 1024, 708, _imageResposity, "bg_main.jpg");

        changeState(PlayState.playStateType.DealingState);
        BufferedImage image = _imageResposity.getImage("NewTextBox.jpg");

        for (int index = 0; index < 4; ++index) {
            int playIndex = (index + 4 - _mainGame.getAdapter().getCurrentUserId()) % 4;
            _names[playIndex] = new LabelSprite(_Xs[playIndex], _Ys[playIndex], 80, 30, _mainGame.getAdapter().getUserName(index), image);
        }

        for (int index = 0; index < 4; ++index) {
            int playIndex = (index + 4 - _mainGame.getAdapter().getCurrentUserId()) % 4;
            _scores[playIndex] = new LabelSprite(_Xs[playIndex], _Ys[playIndex] + 35, 80, 30, String.valueOf(_mainGame.getAdapter().getMarkOfPlayers()[index]), image);
        }
        _pointInHandSprite = new LabelSprite(805, 320, "Point in hand : " + String.valueOf(_mainGame.getAdapter().getPointInHand()), null, Color.yellow);
        _notifySprite = new LabelSprite(805, 280, "", null, Color.yellow);

    }

    public void updateMark() {
        for (int index = 0; index < 4; ++index) {
            int playIndex = (index + 4 - _mainGame.getAdapter().getCurrentUserId()) % 4;
            _scores[playIndex]._text = String.valueOf(_mainGame.getAdapter().getMarkOfPlayers()[index]);
        }
    }

    public void notify(String text) {
        _notifySprite._text = text;
    }

    public void gameEnd() {
    }
    public SynMouseRect _clickedPosition = new SynMouseRect();

    public void mouseClicked(MouseEvent e) {
        Rectangle rect = new Rectangle(e.getX() - _mainGame.getInsets().left, e.getY() - _mainGame.getInsets().top, 1, 1);
        _clickedPosition.setRect(rect);
    }

    public void keyPressed(KeyEvent e) {
    }
}